#  Top Makefile 

#Set Compiler
CC=gcc


#Get SubDirs And Delete out directory
SUBDIRS= main src

#DEBUG=$(shell ls -l|grep ^d |awk '{if($$9 == "out") print $$9}')

#Set and Create output directory and subdir
OUTDIR=out

#Set inc Directory
INC=inc/

#Set Root Directory
ROOT_DIR=$(shell pwd)

#Target bin name
TARGET_NAME=awinicMec

#objects file
OBJS_DIR=${OUTDIR}/objs

#Test data Directory
DATA_DIR =${OUTDIR}/data

#target bin Directory
TARGET_DIR=${OUTDIR}/bin
TARGET_BIN=${ROOT_DIR}/${TARGET_DIR}/${TARGET_NAME}

CFLAGS= -g -Wall -I${ROOT_DIR}/${INC} -lm

#Get Current Directory source file
CUR_SRCS=$(wildcard *.c)
CUR_OBJS=$(patsubst %.c,%.o,${CUR_SRCS})

#translate var to sub Makefile
export CC ROOT_DIR OBJS_DIR TARGET_DIR TARGET_NAME DATA_DIR  CFLAGS

all: PREPARE  ${SUBDIRS} ${CUR_OBJS}  TARGET ${TARGET_BIN}
$(SUBDIRS):ECHO
	-make -C $@

${CUR_OBJS}:%.o:%.c
	${CC} ${CFLAGS} -c $^ -o $(ROOT_DIR)/$(OBJS_DIR)/$@
TARGET:
	$(CC) -o $(TARGET_BIN) $(OBJS_DIR)/*.o ${CFLAGS}

#${TARGET_BIN}:${ROOT_DIR}/${OBJS_DIR}/*.o
#	$(CC) -o $@ $^ ${CFLAGS}

ECHO:
	@echo ${SUBDIRS}

PREPARE:
	@mkdir -p ${ROOT_DIR}/$(OUTDIR)
	@mkdir -p ${ROOT_DIR}/$(DATA_DIR)
	@mkdir -p ${ROOT_DIR}/$(OBJS_DIR)
	@mkdir -p ${ROOT_DIR}/$(TARGET_DIR)
	cp ${ROOT_DIR}/data  ${ROOT_DIR}/${OUTDIR}/ -rf

.PHONY:clean
clean:
	rm -rf ${OBJS_DIR}/*.o
	rm -rf ${TARGET_DIR}/*
	rm -rf ${OUTDIR}/data -rf 


